home *** CD-ROM | disk | FTP | other *** search
/ One Click 11 / OneClick11.iso / Bancos de Dados / Conversao / Mysql2Excel / Setup.exe / Mysql2Excel.exe / MySQLdb / converters.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-06-23  |  4.7 KB  |  111 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. """MySQLdb type conversion module
  5.  
  6. This module handles all the type conversions for MySQL. If the default
  7. type conversions aren't what you need, you can make your own. The
  8. dictionary conversions maps some kind of type to a conversion function
  9. which returns the corresponding value:
  10.  
  11. Key: FIELD_TYPE.* (from MySQLdb.constants)
  12. Conversion function:
  13.      Arguments: string
  14.      Returns: Python object
  15.  
  16. Key: Python type object (from types) or class
  17. Conversion function:
  18.      Arguments: Python object of indicated type or class AND 
  19.                 conversion dictionary
  20.      Returns: SQL literal value
  21.      Notes: Most conversion functions can ignore the dictionary, but
  22.             it is a required parameter. It is necessary for converting
  23.             things like sequences and instances.
  24.  
  25. Don't modify conversions if you can avoid it. Instead, make copies
  26. (with the copy() method), modify the copies, and then pass them to
  27. MySQL.connect().
  28.  
  29. """
  30. from _mysql import string_literal, escape_sequence, escape_dict, escape, NULL
  31. from constants import FIELD_TYPE
  32. from sets import *
  33. from times import *
  34. from string import split
  35. import types
  36.  
  37. def Str2Set(s):
  38.     values = split(s, ',')
  39.     return apply(Set, tuple(values))
  40.  
  41.  
  42. def Thing2Str(s, d):
  43.     '''Convert something into a string via str().'''
  44.     return str(s)
  45.  
  46. if str(0x0L)[-1] == 'L':
  47.     
  48.     def Long2Int(l, d):
  49.         '''Convert a long integer to a string, chopping the L.'''
  50.         return str(l)[:-1]
  51.  
  52. else:
  53.     Long2Int = Thing2Str
  54.  
  55. def None2NULL(o, d):
  56.     '''Convert None to NULL.'''
  57.     return NULL
  58.  
  59.  
  60. def Thing2Literal(o, d):
  61.     '''Convert something into a SQL string literal.  If using
  62.     MySQL-3.23 or newer, string_literal() is a method of the
  63.     _mysql.MYSQL object, and this function will be overridden with
  64.     that method when the connection is created.'''
  65.     return string_literal(o, d)
  66.  
  67.  
  68. def Instance2Str(o, d):
  69.     """Convert an Instance to a string representation.  If the
  70.     __str__() method produces acceptable output, then you don't need
  71.     to add the class to conversions; it will be handled by the default
  72.     converter. If the exact class is not found in d, it will use the
  73.     first class it can find for which o is an instance."""
  74.     if d.has_key(o.__class__):
  75.         return d[o.__class__](o, d)
  76.     
  77.     cl = filter((lambda x, o = o: if type(x) == types.ClassType:
  78. passisinstance(o, x)), d.keys())
  79.     if not cl:
  80.         return d[types.StringType](o, d)
  81.     
  82.     d[o.__class__] = d[cl[0]]
  83.     return d[cl[0]](o, d)
  84.  
  85. conversions = {
  86.     types.IntType: Thing2Str,
  87.     types.LongType: Long2Int,
  88.     types.FloatType: Thing2Str,
  89.     types.NoneType: None2NULL,
  90.     types.TupleType: escape_sequence,
  91.     types.ListType: escape_sequence,
  92.     types.DictType: escape_dict,
  93.     types.InstanceType: Instance2Str,
  94.     types.StringType: Thing2Literal,
  95.     DateTimeType: DateTime2literal,
  96.     DateTimeDeltaType: DateTimeDelta2literal,
  97.     FIELD_TYPE.TINY: int,
  98.     FIELD_TYPE.SHORT: int,
  99.     FIELD_TYPE.LONG: long,
  100.     FIELD_TYPE.FLOAT: float,
  101.     FIELD_TYPE.DOUBLE: float,
  102.     FIELD_TYPE.DECIMAL: float,
  103.     FIELD_TYPE.LONGLONG: long,
  104.     FIELD_TYPE.INT24: int,
  105.     FIELD_TYPE.YEAR: int,
  106.     FIELD_TYPE.SET: Str2Set,
  107.     FIELD_TYPE.TIMESTAMP: mysql_timestamp_converter,
  108.     FIELD_TYPE.DATETIME: DateTime_or_None,
  109.     FIELD_TYPE.TIME: TimeDelta_or_None,
  110.     FIELD_TYPE.DATE: Date_or_None }
  111.